package com.panshi.controller.user;

import com.panshi.api.*;
import com.panshi.po.*;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心
 */
@Controller
@Slf4j
public class CenterController {

    @Autowired
    private UserApi userApi;//用户



    @Autowired
    private ArticleApi articleApi;//文章

    @Autowired
    private QuestionApi questionApi;//提问

    @Autowired
    private  CollectApi collectApi;//收藏


    @Autowired
    private  QuestionAuditApi questionAuditApi;//问题审核api

    @Autowired
    private  AnswerAuditApi answerAuditApi;//回答审核api

    @Autowired
    private ArticleauditApi articleauditApi;//文章审核api




    @GetMapping("center/{email}")
    public ModelAndView informationCenter(@PathVariable String email, HttpServletRequest httpRequest){
        ModelAndView   modelAndView=new ModelAndView("user/userCenter");

        UserPO userPO= userApi.findUserByEmail(email).getData();
        httpRequest.setAttribute("userPO",userPO);



        //提问总条数
        Integer  findSumByUid=questionAuditApi.findCount(4).getData();
        log.info("提问==========={}",findSumByUid);
        modelAndView.addObject("findSumByUid",findSumByUid);



        //回答总条数
        Integer uidTotal= answerAuditApi.answerAuditUidTotal(4).getData();
        log.info("回答in==========={}",uidTotal);
        modelAndView.addObject("uidTotal",uidTotal);




        //文章总条数
        Integer uidQuery=articleauditApi.uidCount(4);
        log.info("文章uidQuery=========={}",uidQuery);
        modelAndView.addObject("uidQuery",uidQuery);

        //收藏总条数
        Integer uidCount= collectApi.uidCount(4);
        log.info("收藏integer2==========={}",uidCount);
        modelAndView.addObject("uidCount",uidCount);




        //提问审核内容
        FindByUidVO findByUidVO=new FindByUidVO();
        findByUidVO.setUid(4);
        findByUidVO.setCurrentPage(1);
        findByUidVO.setCurrentPageSize(2);
        List<QuestionAuditPO>  questionAuditPOS=questionAuditApi.findQuestionByUid(findByUidVO).getData();
        log.info("提问list========={}",questionAuditPOS);
        modelAndView.addObject("questionAuditPOS",questionAuditPOS);

        //回答审核内容
        AnswerAuditUidQuery answerAuditUidQuery=new AnswerAuditUidQuery();
        answerAuditUidQuery.setUid(4);
        answerAuditUidQuery.setCurrenPage(1);
        answerAuditUidQuery.setSize(2);
        List<AnswerAuditPO>  answerAuditPOS=answerAuditApi.answerAuditUid(answerAuditUidQuery).getData();
        log.info("回答answerPOS=========={}",answerAuditPOS);
        modelAndView.addObject("answerAuditPOS",answerAuditPOS);

        //文章审核内容
        ArticleauditUidQueryVo articleauditUidQueryVo=new ArticleauditUidQueryVo();
        articleauditUidQueryVo.setUid(4);
        articleauditUidQueryVo.setCurrentPage(1);
        articleauditUidQueryVo.setPageSize(2);
        List<ArticleauditPo> articleauditPos=articleauditApi.uidQuery(articleauditUidQueryVo).getData();
        log.info("文章articlePos========{}",articleauditPos);
        modelAndView.addObject("articleauditPos",articleauditPos);

        return modelAndView;
    }


    //个人主页
    @GetMapping("/ce/{num}/{email}")
    public ModelAndView ce(@PathVariable String num,@PathVariable String email, HttpServletRequest httpRequest){

        ModelAndView  modelAndView=new ModelAndView("user/userCenter-list");

        UserPO userPO= userApi.findUserByEmail(email).getData();
        httpRequest.setAttribute("userPO",userPO);

        if("0".equals(num)){
            //提问审核内容
            FindByUidVO findByUidVO=new FindByUidVO();
            findByUidVO.setUid(4);
            findByUidVO.setCurrentPage(1);
            findByUidVO.setCurrentPageSize(2);
            List<QuestionAuditPO>  questionPOS=questionAuditApi.findQuestionByUid(findByUidVO).getData();
            log.info("提问list========={}",questionPOS);
            modelAndView.addObject("questionPOS",questionPOS);

            //回答审核内容
            AnswerAuditUidQuery answerAuditUidQuery=new AnswerAuditUidQuery();
            answerAuditUidQuery.setUid(4);
            answerAuditUidQuery.setCurrenPage(1);
            answerAuditUidQuery.setSize(2);
            List<AnswerAuditPO>  answerPOS=answerAuditApi.answerAuditUid(answerAuditUidQuery).getData();
            log.info("回答answerPOS=========={}",answerPOS);
            modelAndView.addObject("answerPOS",answerPOS);

            //文章审核内容
            ArticleauditUidQueryVo articleauditUidQueryVo=new ArticleauditUidQueryVo();
            articleauditUidQueryVo.setUid(4);
            articleauditUidQueryVo.setCurrentPage(1);
            articleauditUidQueryVo.setPageSize(2);
            List<ArticleauditPo> articlePos=articleauditApi.uidQuery(articleauditUidQueryVo).getData();
            log.info("文章articlePos========{}",articlePos);
            modelAndView.addObject("articlePos",articlePos);

        }else if("1".equals(num)){
            //提问审核内容
            FindByUidVO findByUidVO=new FindByUidVO();
            findByUidVO.setUid(4);
            findByUidVO.setCurrentPage(1);
            findByUidVO.setCurrentPageSize(2);
            List<QuestionAuditPO>  questionPOS=questionAuditApi.findQuestionByUid(findByUidVO).getData();
            log.info("提问list========={}",questionPOS);
            modelAndView.addObject("questionPOS",questionPOS);

        }else if("2".equals(num)){
            //回答审核内容
            AnswerAuditUidQuery answerAuditUidQuery=new AnswerAuditUidQuery();
            answerAuditUidQuery.setUid(4);
            answerAuditUidQuery.setCurrenPage(1);
            answerAuditUidQuery.setSize(2);
            List<AnswerAuditPO>  answerPOS=answerAuditApi.answerAuditUid(answerAuditUidQuery).getData();
            log.info("回答answerPOS=========={}",answerPOS);
            modelAndView.addObject("answerPOS",answerPOS);
        }else if("3".equals(num)){
            //文章审核内容
            ArticleauditUidQueryVo articleauditUidQueryVo=new ArticleauditUidQueryVo();
            articleauditUidQueryVo.setUid(4);
            articleauditUidQueryVo.setCurrentPage(1);
            articleauditUidQueryVo.setPageSize(2);
            List<ArticleauditPo> articlePos=articleauditApi.uidQuery(articleauditUidQueryVo).getData();
            log.info("文章articlePos========{}",articlePos);
            modelAndView.addObject("articlePos",articlePos);
        }else if("4".equals(num)) {
            //收藏
            CollectUidQueryVo collectUidQueryVo = new CollectUidQueryVo();
            collectUidQueryVo.setUid(4);
            collectUidQueryVo.setCurrentPage(1);
            collectUidQueryVo.setPageSize(2);
            List<CollectPo> collectPo = collectApi.uidContent(collectUidQueryVo).getData();
            log.info("收藏collectPos========={}", collectPo);
            List<ArticleanswerVO> list = new ArrayList<>();

            for (int i = 0; i < collectPo.size(); i++) {
                //文章获取每一个nid
                ArticleNumberQueryVo articleNumberQueryVo = new ArticleNumberQueryVo();
                articleNumberQueryVo.setNumber(collectPo.get(i).getNid());

                //根据nid查询文章信息
                ResponseVO<ArticlePo> articlePo = articleApi.numberQuery(articleNumberQueryVo);
                ArticleanswerVO articleanswerVO = new ArticleanswerVO();
                articleanswerVO.setArticlePo(articlePo.getData());

                list.add(articleanswerVO);
            }
            httpRequest.setAttribute("collectPos",list);
        }

        return modelAndView;
    }











}
